package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;

import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.Arrays;
import java.util.List;

@Api("用户信息管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class userController {
    private final IUserService userService;

    @ApiOperation("新增用户")
    @PostMapping
    public void save(@RequestBody UserFormDTO userFormDTO) {
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }
    /*
    * @param ids
    * @return 批量查询用户
    * */
    @ApiOperation("根据id批量查询用户")
    @GetMapping
    public List<UserVO> queryAll(@RequestParam Long[] ids) {
        List<User> users = userService.listByIds(Arrays.asList(ids));
        return BeanUtil.copyToList(users, UserVO.class);
    }

    @ApiOperation("根据条件进行查询")
    @PostMapping("/list")
    public List<UserVO> queryList(@RequestBody UserQuery userQuery) {
        return  userService.queryList(userQuery);
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        System.out.println("1111"+ response);
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("template/itcast.xlsx");
        //返回resourceAsStream这个文件的路径
//        XSSFWorkbook excel = new XSSFWorkbook(resourceAsStream);
        XSSFWorkbook excel = new XSSFWorkbook();
        //获取第一个sheet
//        XSSFSheet sheetAt = excel.getSheetAt(0);
//        String stringCellValue = sheetAt.getRow(0).getCell(1).getStringCellValue();
//        System.out.println("stringCellValue = " + stringCellValue);

        //导出
//        XSSFSheet sheetAt = excel.getSheetAt(0);
////        sheetAt.getRow(0).getCell(0).setCellValue("测试1");
//        sheetAt.getRow(0).getCell(1).setCellValue("测试2");
////        FileOutputStream f = new FileOutputStream("D:/itcast.xlsx");
//        ServletOutputStream outputStream = response.getOutputStream();
//        excel.write(outputStream);
//        outputStream.flush();
//        outputStream.close();
//        excel.close();
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=users.xlsx");
        XSSFSheet sheet1 = excel.createSheet("sheet1");
        XSSFRow row = sheet1.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("用户名");
        ServletOutputStream outputStream = response.getOutputStream();
//        FileOutputStream f = new FileOutputStream("D:/itcast.xlsx");
        excel.write(outputStream);

//        return stringCellValue;
    }
}
